package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.model.bean.BookCoverInfo;
import com.shuqi.model.bean.BookCoverNewChapterInfo;
import com.shuqi.model.bean.BookCoverOtherBookInfo;
import com.shuqi.model.bean.BookTagInfo;
import com.shuqi.model.parse.HandlerBase;

public class BookCoverParser extends HandlerBase {

    private BookCoverInfo info;

    private List<BookCoverOtherBookInfo> otherBookList;
    private BookCoverOtherBookInfo otherBook;

    private List<BookTagInfo> tagList;
    private BookTagInfo tag;

    private BookCoverNewChapterInfo newChapter;

    private int inWhich;

    private final int BOOKID = 1; // 书籍id
    private final int BOOKNAME = 2; // 书名
    private final int AUTHOR = 3; // 作者
    private final int BOOKTYPE = 4; // 书籍类型
    private final int BookState = 5; // 书籍类型
    private final int WORDS = 6; // 书籍总字数
    private final int CHAPTERCOUNT = 7; // 章节数量
    private final int FIRSTCHAPTERID = 8;// 书籍第一章ID
    private final int IMAGEURL = 9; // 封面图url
    private final int Discount = 10; // 封面图url
    private final int PRICE = 11; // 当前价格（逻辑判断使用此价格）
    private final int OLDPRICE = 12;// 之前价格(仅作显示使用)
    private final int DESCRIPTION = 13; // 书籍简介
    private final int BookTags = 14; // 书籍简介
    private final int BookTag = 15; // 书籍简介
    private final int NewChapter = 16; // 书籍简介
    private final int ChapterId = 17; // 书籍简介
    private final int ChapterName = 18; // 书籍简介
    private final int UpdateTime = 19; // 书籍简介
    private final int OtherBooks = 20; // 书籍简介
    private final int OtherBook = 21; // 书籍简介
    private final int OBookName = 22; // 书籍简介
    private final int OAuthor = 23; // 书籍简介
    private final int OBookType = 24; // 书籍简介
    private final int ODescription = 25; // 书籍简介
    private final int BOOKMAXOID = 26; // 书籍简介

    @Override
    public void startDocument() throws SAXException {
        info = new BookCoverInfo();
        inWhich = 0;
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    public BookCoverInfo getParsedData() {
        return info;
    }

    public void startElement(String uri, String localName, String qName, Attributes atts) {
        if (localName.equals("Book")) {
            info.setHide("Y".equals(isNull(atts, "hide")));
            info.setCoverOpen("1".equals(isNull(atts, "cr_cover_isopen")));
            info.setReadOpen("1".equals(isNull(atts, "cr_read_isopen")));
            info.setPayMode(Integer.parseInt(isNull(atts, "payMode")));
            info.setPaid(isNull(atts, "paid"));
            info.setCollected(atts.getValue("collected"));
        } else if (localName.equals("BookId")) {
            inWhich = BOOKID;
        } else if (localName.equals("BookName")) {
            inWhich = BOOKNAME;
        } else if (localName.equals("Author")) {
            info.setAuthorId(isNull(atts, "authorId"));
            inWhich = AUTHOR;
        } else if (localName.equals("BookType")) {
            info.setBookTypeId(isNull(atts, "typeId"));
            inWhich = BOOKTYPE;
        } else if (localName.equals("BookState")) {
            inWhich = BookState;
        } else if (localName.equals("Words")) {
            inWhich = WORDS;
        } else if (localName.equals("ChapterCount")) {
            inWhich = CHAPTERCOUNT;
        } else if (localName.equals("FirstChapterId")) {
            inWhich = FIRSTCHAPTERID;
        } else if (localName.equals("Discount")) {
            inWhich = Discount;
        } else if (localName.equals("ImageUrl")) {
            inWhich = IMAGEURL;
        } else if (localName.equals("Price")) {
            inWhich = OLDPRICE;
        } else if (localName.equals("DiscountPrice")) {
            inWhich = PRICE;
        } else if (localName.equals("Description")) {
            inWhich = DESCRIPTION;
        } else if (localName.equals("Tags")) {
            tagList = new ArrayList<BookTagInfo>();
            inWhich = BookTags;
        } else if (localName.equals("Tag")) {
            tag = new BookTagInfo();
            tag.setId(isNull(atts, "keyWordId"));
            tag.setName(isNull(atts, "keyWordName"));
            tagList.add(tag);
            inWhich = BookTag;
        } else if (localName.equals("NewChapter")) {
            newChapter = new BookCoverNewChapterInfo();
            inWhich = NewChapter;
        } else if (localName.equals("ChapterId")) {
            inWhich = ChapterId;
        } else if (localName.equals("ChapterName")) {
            inWhich = ChapterName;
        } else if (localName.equals("UpdateTime")) {
            inWhich = UpdateTime;
        } else if (localName.equals("OtherBooks")) {
            otherBookList = new ArrayList<BookCoverOtherBookInfo>();
            inWhich = OtherBooks;
        } else if (localName.equals("OtherBook")) {
            otherBook = new BookCoverOtherBookInfo();
            otherBook.setBookId(isNull(atts, "bookId"));
            otherBook.setImgUrl(isNull(atts, "imgUrl"));
            otherBook.setHide("Y".equals(isNull(atts, "hide")));
            otherBook.setCoverOpen("1".equals(isNull(atts, "cr_cover_isopen")));
            otherBook.setReadOpen("1".equals(isNull(atts, "cr_read_isopen")));
            inWhich = OtherBook;
        } else if (localName.equals("OBookName")) {
            inWhich = OBookName;
        } else if (localName.equals("OAuthor")) {
            inWhich = OAuthor;
        } else if (localName.equals("OBookType")) {
            inWhich = OBookType;
        } else if (localName.equals("ODescription")) {
            inWhich = ODescription;
        } else if(localName.equals("MaxOid")){
            inWhich = BOOKMAXOID;
        }else {
            inWhich = 0;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equals("Tags")) {
            info.setTagList(tagList);
        } else if (localName.equals("NewChapter")) {
            info.setNewChapter(newChapter);
        } else if (localName.equals("OtherBook")) {
            otherBookList.add(otherBook);
        } else if (localName.equals("OtherBooks")) {
            info.setOtherList(otherBookList);
        } else if (localName.equals("Book")) {
            info.setOtherList(otherBookList);
        }
        super.endElement(uri, localName, qName);
    }

    public void characters(char[] ch, int start, int length) {
        String value = new String(ch, start, length);
        if (value != null && !value.trim().equals("")) {
            switch (inWhich) {
                case BOOKID:
                    if (info.getBookId() != null) {
                        info.setBookId(info.getBookId() + value);
                    } else {
                        info.setBookId(value);
                    }
                    break;
                case BOOKNAME:
                    if (info.getBookName() != null) {
                        info.setBookName(info.getBookName() + value);
                    } else {
                        info.setBookName(value);
                    }
                    break;
                case AUTHOR:
                    if (info.getAuthor() != null) {
                        info.setAuthor(info.getAuthor() + value);
                    } else {
                        info.setAuthor(value);
                    }
                    break;
                case BOOKTYPE:
                    if (info.getBookType() != null) {
                        info.setBookType(info.getBookType() + value);
                    } else {
                        info.setBookType(value);
                    }
                    break;
                case BookState:
                    if (info.getBookState() != null) {
                        info.setBookState(info.getBookState() + value);
                    } else {
                        info.setBookState(value);
                    }
                    break;
                case WORDS:
                    if (info.getBookWords() != null) {
                        info.setBookWords(info.getBookWords() + value);
                    } else {
                        info.setBookWords(value);
                    }
                    break;
                case CHAPTERCOUNT:
                    if (info.getChapterCount() != null) {
                        info.setChapterCount(info.getChapterCount() + value);
                    } else {
                        info.setChapterCount(value);
                    }
                    break;
                case FIRSTCHAPTERID:
                    if (info.getFirstChapterId() != null) {
                        info.setFirstChapterId(info.getFirstChapterId() + value);
                    } else {
                        info.setFirstChapterId(value);
                    }
                    break;
                case IMAGEURL:
                    if (info.getImageUrl() != null) {
                        info.setImageUrl(info.getImageUrl() + value);
                    } else {
                        info.setImageUrl(value);
                    }
                    break;
                case Discount:
                    if (info.getDiscount() != null) {
                        info.setDiscount(info.getDiscount() + value);
                    } else {
                        info.setDiscount(value);
                    }
                    break;
                case PRICE:
                    if (info.getPrice() != null) {
                        info.setPrice(info.getPrice() + value);
                    } else {
                        info.setPrice(value);
                    }
                    break;
                case OLDPRICE:
                    if (info.getOldPrice() != null) {
                        info.setOldPrice(info.getOldPrice() + value);
                    } else {
                        info.setOldPrice(value);
                    }
                    break;
                case DESCRIPTION:
                    if (info.getDescription() != null) {
                        info.setDescription(info.getDescription() + value);
                    } else {
                        info.setDescription(value);
                    }
                    break;

                // 最新章节
                case ChapterId:
                    if (newChapter.getChapterID() != null) {
                        newChapter.setChapterID(newChapter.getChapterID() + value);
                    } else {
                        newChapter.setChapterID(value);
                    }
                    break;
                case ChapterName:
                    if (newChapter.getChapterName() != null) {
                        newChapter.setChapterName(newChapter.getChapterName() + value);
                    } else {
                        newChapter.setChapterName(value);
                    }
                    break;
                case UpdateTime:
                    if (newChapter.getUpdateTime() != null) {
                        newChapter.setUpdateTime(newChapter.getUpdateTime() + value);
                    } else {
                        newChapter.setUpdateTime(value);
                    }
                    break;

                // 推荐书籍
                case OBookName:
                    if (otherBook.getBookName() != null) {
                        otherBook.setBookName(otherBook.getBookName() + value);
                    } else {
                        otherBook.setBookName(value);
                    }
                    break;
                case OAuthor:
                    if (otherBook.getAuthor() != null) {
                        otherBook.setAuthor(otherBook.getAuthor() + value);
                    } else {
                        otherBook.setAuthor(value);
                    }
                    break;
                case OBookType:
                    if (otherBook.getBookType() != null) {
                        otherBook.setBookType(otherBook.getBookType() + value);
                    } else {
                        otherBook.setBookType(value);
                    }
                    break;
                case ODescription:
                    if (otherBook.getDescription() != null) {
                        otherBook.setDescription(otherBook.getDescription() + value);
                    } else {
                        otherBook.setDescription(value);
                    }
                    break;
                case BOOKMAXOID:
                    if (info.getMaxOid() != null) {
                        info.setMaxOid(info.getMaxOid()+ value);
                    } else {
                        info.setMaxOid(value);
                    }
                    break;
            }
        }
    }
}
